package com.erp.erp_mask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.erp_mask.pojo.TBom;
import com.erp.erp_mask.service.TBomService;
import com.erp.erp_mask.mapper.TBomMapper;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author yang
* @description 针对表【t_bom】的数据库操作Service实现
* @createDate 2024-05-17 16:36:56
*/
@Service
public class TBomServiceImpl extends ServiceImpl<TBomMapper, TBom>
    implements TBomService{
    private List<TBom> findwhere(Integer level){
        LambdaQueryWrapper<TBom> wrapper = Wrappers.lambdaQuery();
        if (level==null){
            wrapper.isNull(TBom::getBomLevel);
        }else{
            wrapper.eq(TBom::getBomLevel,level);
        }
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<TBom> findById() {
        List<TBom> findwhered = this.findwhere(null);
        Map<Integer,TBom> bomMap = new HashMap<>();
        for(TBom bom:findwhered){
            bomMap.put(bom.getBomId(),bom);
        }
        for (TBom bom: findwhered){
            List<TBom> children = this.findwhere(bom.getBomId());
            bom.setChildren(children);
            for (TBom child:children){
                List<TBom> grandChildren = this.findwhere(child.getBomId());
                child.setChildren(grandChildren);
            }
        }
        return findwhered;
    }

    @Override
    public void faddlist(TBom tBom) {
        baseMapper.faddlist(tBom);
    }

    @Override
    public List<TBom> selectflist(Integer level) {
        return baseMapper.selectflist(level);
    }

    @Override
    public List<TBom> cascaderlist() {
        List<TBom> findTree = this.findwhere(null);
        Map<Integer,TBom> bomMap = new HashMap<>();
        for (TBom bom:findTree){
            bomMap.put(bom.getBomId(),bom);
        }
        for (TBom bom :findTree){
            List<TBom> children = this.findwhere(bom.getBomId());
            bom.setChildren(children);
            for (TBom child:children){
                List<TBom> grandChildren = this.findwhere(child.getBomId());
                child.setChildren(grandChildren);
            }
        }
        return findTree;
    }
}




